<?php
class FT_Post {
	private $info = array();
	private $url = array();
	private $extras;
	
	/* CONSTRUCTOR */
	
	function FT_Post($post_id = null, $extras = false) {
		$this->info['id'] = $post_id;
		$this->extras = $extras;
		
		$this->FT_Crear_Info();
		$this->FT_Crear_Categorias();
		$this->FT_Crear_Info_Extra();
		$this->FT_Crear_URL();
	}
	
	/* METODOS PRIVADOS */
	
	/* Crea la info basica del post */
	
	private function FT_Crear_Info() {
		global $wp_query, $post;
		
		if(is_null($this->info['id'])) :
			if($wp_query->post_count == 1) :
				$this->info['id'] = $wp_query->post->ID;
			elseif($wp_query->post_count == 0) :
				$this->info['id'] = $post->ID;
			endif;
			$this->info['current'] = true;
		else :
			$this->info['current'] = false;
			if(($this->info['id'] == $wp_query->post->ID) || ($this->info['id'] == $post->ID))
				$this->info['current'] = true;
		endif;
		
		if($this->info['current'] && !$this->extras)
			$this->extras = true;
		
		if(!is_null($this->info['id'])) :
			$post_info = get_post($this->info['id']);
			
			$this->info['title'] = $post_info->post_title;
			$this->info['date'] = $post_info->post_date;
			$this->info['author'] = $post_info->post_author;
		endif;
	}
	
	/* Crea las categorias del post */
	
	private function FT_Crear_Categorias() {
		$cats_aux = get_the_category($this->info['id']);
			
		foreach($cats_aux as $cat_aux) :
			$cat = new FT_Cat($cat_aux->cat_ID, false);
			$cats[sanitize_title($cat->FT_Get_Info('name'))] = $cat;
		endforeach;
		
		ksort($cats);
		
		foreach($cats as $cat) :
			$this->info['cats'][$cat->FT_Get_Info('id')] = $cat;
		endforeach;
	}
	
	/* Crea la info extra del post. Solo si este es el post actual */
	
	private function FT_Crear_Info_Extra() {
		if($this->extras) :
			$custom_fields = get_post_custom($this->info['id']);
			
			foreach($custom_fields as $parent_key => $custom_field) :
				foreach($custom_field as $key => $value) :
					if(count($custom_field) > 1)
						$this->info['extra'][$parent_key][$key] = FT_Unserialize($value);
					else
						$this->info['extra'][$parent_key] = FT_Unserialize($value);
				endforeach;
			endforeach;
		endif;
	}
	
	/* Crea las URL del post */
	
	private function FT_Crear_URL() {
		$this->url['permalink'] = get_permalink($this->info['id']);
	}
	
	/* METODOS PUBLICOS */
	
	/* Devuelve la info basica del post */
	
	public function FT_Get_Info($info_name = '') {
		if(empty($info_name))
			return $this->info;
		else
			return $this->info[sanitize_title($info_name)];
	}
	
	/* Devuelve las URL del post */
	
	public function FT_Get_URL($url_name = '') {
		if(empty($url_name))
			return $this->url;
		else
			return $this->url[sanitize_title($url_name)];
	}
	
	/* Determina si es de una categoria dada o no */
	
	public function FT_Is_Cat($cats_name) {
		if(strpos($cats_name, ' y ') === false) :
			$cats = FT_Explode($cats_name, ' o ');
			
			foreach($cats as $cat) :
				foreach($this->info['cats'] as $this_cat) :
					if($cat == $this_cat->FT_Get_Info('name'))
						return true;
				endforeach;
			endforeach;
			
			return false;
		else :
			$cats = FT_Explode($cats_name, ' y ');
			
			foreach($cats as $cat) :
				foreach($this->info['cats'] as $this_cat) :
					if($cat == $this_cat->FT_Get_Info('name')) :
						if(!isset($is_cat))
							$is_cat = true;
						else
							$is_cat = $is_cat && true;
					else :
						if(!isset($is_cat))
							$is_cat = false;
						else
							$is_cat = $is_cat && false;
					endif;
				endforeach;
			endforeach;
			
			return $is_cat;
		endif;
	}
	
	/* Agrega o modifica los datos del metadata del post */
	
	public function FT_Add_Change_Data($data, $meta_name = '', $post_id = null, $serialize = true) {
		if(is_null($post_id))
			$post_id = $this->info['id'];
		
		if(!isset($this->info['extra'][$meta_name])) :
			if(is_array($data) && !$serialize) :
				foreach($data as $key => $value) :
					if(!is_numeric($key))
						$meta_name = $key;
					add_post_meta($post_id, $meta_name, $value, true);
				endforeach;
			else :
				add_post_meta($post_id, $meta_name, FT_Serialize($data), true);
			endif;
		elseif(FT_Serialize($this->info['extra'][$meta_name]) != FT_Serialize($data)) :
			if(is_array($data) && !$serialize) :
				foreach($data as $key => $value) :
					if(!is_numeric($key))
						$meta_name = $key;
					update_post_meta($post_id, $meta_name, $value);
				endforeach;
			else :
				update_post_meta($post_id, $meta_name, FT_Serialize($data), true);
			endif;
		endif;
	}
	
	/* Elimina datos del metadata del post */
	
	public function FT_Del_Data($meta_name, $post_id = null) {
		if(is_null($post_id))
			$post_id = $this->info['id'];
		
		delete_post_meta($post_id, $meta_name);
	}
}
?>